/**
 * @author Gregory Banfi
 */

import sa4.MainFramework
import sa4.Form
import org.junit._
import org.junit.Assert._
import com.meterware.servletunit.ServletRunner
import com.meterware.httpunit.PostMethodWebRequest
import com.meterware.httpunit.GetMethodWebRequest
import com.meterware.httpunit.WebRequest
import com.meterware.servletunit.ServletUnitClient
import com.meterware.httpunit.WebResponse
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpServletRequest

class testMainFramework {

  val ss = new ServletRunner()
  ss.registerServlet("", classOf[MainFramework].getName)

  //assertNotNull("No response received", response)
  //assertTrue(response.getResponseCode == 404)

  @Test
  def testSession() {
    //we make to request and check that they are different
    //this because the finishSession method that create a new 
    //session every time a request is sent in the response
    //there is the session id, so we test that is different
    val sc: ServletUnitClient = ss.newClient()
    //request 1
    val request: WebRequest = new GetMethodWebRequest("http://localhost/")
    val response: WebResponse = sc.getResponse(request)
    var respText = response.getText()
    //request 2
    val request2: WebRequest = new GetMethodWebRequest("http://localhost/")
    val response2: WebResponse = sc.getResponse(request2)
    var respText2 = response2.getText()
    assertFalse(respText.equals(respText2))
  }

  @Test
  def testPersistance() {
    val st: ServletUnitClient = ss.newClient()
    //request to the page where we can add and remove books

    {
      val request: WebRequest = new GetMethodWebRequest("http://localhost/bookShop")
      val response: WebResponse = st.getResponse(request)
      assertNotNull("No response received", response)
      val xml = scala.xml.XML.loadString(response.getText)
    } //request to the list of books to see that there are only the books we initialized
    {
      val request: WebRequest = new GetMethodWebRequest("http://localhost/listBooks")
      val response: WebResponse = st.getResponse(request)
      assertFalse(response.getText.contains("Robinson Crusoe"))
    } //add a book to the list (database)
    {
      val request: WebRequest = new PostMethodWebRequest("http://localhost/postBook")
      request.setParameter("addTitle", "Robinson Crusoe")
      request.setParameter("author", "Daniel Defoe")
      val response: WebResponse = st.getResponse(request)
    } //test that the book is really there
    {
      val request: WebRequest = new GetMethodWebRequest("http://localhost/listBooks")
      val response: WebResponse = st.getResponse(request)
      assertTrue(response.getText.contains("Robinson Crusoe"))
    } //remove the book we added
    {
      val request: WebRequest = new PostMethodWebRequest("http://localhost/removeBook")
      request.setParameter("removeTitle", "Robinson Crusoe")
      val response: WebResponse = st.getResponse(request)
    } //test that we really removed the book
    {
      val request: WebRequest = new GetMethodWebRequest("http://localhost/listBooks")
      val response: WebResponse = st.getResponse(request)
      assertFalse(response.getText.contains("Robinson Crusoe"))
    }
  }
}